<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\Store;

use App\Constants\Constants;
use App\Dao\IntegralDetailDao;
use App\Dao\UserDao;
use App\Job\IntegralJob;
use App\Kernel\Util\StoreJwtInstance;
use App\Service\Service;

use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

/**
 * 用户服务
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\Admin
 */
class UserService extends Service
{
    /**
     * 后台用户
     *
     * @Inject
     * @var UserDao
     */
    public $user_dao;

    /**
     * @Inject
     * @var IntegralDetailDao
     */
    public $integral_detail_dao;

    /**
     * 会员列表
     * @param array $params
     * @return mixed
     */
    public function list(array $params = [])
    {
        $map = [];

        isset($params['start_time']) && !isset($params['end_time']) && !empty($params['start_time']) && $map['created_at'] = ['>=', strtotime($params['start_time'])]; // 注册开始时间
        isset($params['end_time']) &&  !isset($params['start_time'])&& !empty($params['end_time'])  && $map['created_at'] = ['<=', strtotime($params['end_time'])]; // 注册结束时间

        isset($params['end_time']) &&  isset($params['start_time'])
        && !empty($params['end_time']) && !empty($params['start_time'])
        && $map['between_field'] = ['created_at' =>[strtotime($params['start_time']), strtotime($params['end_time'])]]; // 有注册开始和结束时间

        isset($params['phone']) && !empty($params['phone']) && $map['phone'] = ['like',"%{$params['phone']}%"]; // 账号
        isset($params['identity_code']) && !empty($params['identity_code']) && $map['identity_code'] = ['like',"%{$params['identity_code']}%"]; // 邀请码
        isset($params['search']) && !empty($params['search']) && $map['name'] = ['like',"%{$params['search']}%"]; // 搜索用户名 /姓名

        isset($params['province_id']) && !empty($params['province_id']) && $map['province_id'] = (int)$params['province_id']; // 省级pid
        isset($params['city_id']) && !empty($params['city_id']) && $map['city_id'] = (int)$params['city_id']; // 市级pid
        isset($params['district_id']) && !empty($params['district_id']) && $map['district_id'] = (int)$params['district_id']; // 区级pid

        $select = ['id','created_at','name','phone','district','identity_code','parent_id','group_id','integral'];

        // 分页
        $page_size = null;
        if (isset($params['page'])) {
            $page_size = 10;
            isset($params['page_size']) && !empty($page_size) && $page_size = (int)$params['page_size'];
        }
        $sort = 'id desc';
        $result = $this->user_dao->getUsersByMap($map, $sort, $select, $page_size)->toArray();

        $data = [];
       if( $result ) {
           if (isset($result['data'])) {
               foreach ($result['data'] as $key => $value) {
                   $parent = $this->user_dao->first($value['parent_id'], false);
                   $data[$key]['parent_name'] = $parent['name'] ?? '';
                   $data[$key]['parent_group_id'] = $parent['group_id'] ?? '';
                   $data[$key]['id'] = $value['id'];
                   $data[$key]['created_at'] = date('Y.m.d', $value['created_at']);
                   $data[$key]['name'] = $value['name'];
                   $data[$key]['phone'] = $value['phone'];
                   $data[$key]['district'] = $value['district'] ?? "";
                   $data[$key]['identity_code'] = $value['identity_code'] ?? "";
                   $data[$key]['group_id'] = $value['group_id'] ?? 0;
                   $data[$key]['integral'] = $value['integral'] ?? 0;

               }
           } else {
               foreach ($result as $key => $value) {
                   $parent = $this->user_dao->first($value['parent_id'], false);
                   $data[$key]['parent_name'] = $parent['name'] ?? '';
                   $data[$key]['parent_group_id'] = $parent['group_id'] ?? '';
                   $data[$key]['id'] = $value['id'];
                   $data[$key]['created_at'] = date('Y.m.d', $value['created_at']);
                   $data[$key]['name'] = $value['name'];
                   $data[$key]['phone'] = $value['phone'];
                   $data[$key]['district'] = $value['district'] ?? "";
                   $data[$key]['identity_code'] = $value['identity_code'] ?? "";
                   $data[$key]['group_id'] = $value['group_id'] ?? 0;
                   $data[$key]['integral'] = $value['integral'] ?? 0;

               }
           }
         // 分页情况
           if ($page_size) {
               $result['data'] = $data;
           } else {
               $result = $data;
           }

       }

       return $result;
    }

    /**
     *会员积分增加
     * @param array $param
     * @return  mixed
     */
    public function integralAdd(array $param)
    {
        $result = $this->integral_detail_dao->create([
            'user_id' => $param['user_id'],
            'status' => 3,
            'type' => $this->integral_detail_dao::INCOME_TYPE,
            'num' => filterMoney($param['num'],4),
            'remark' => $param['remark'],
        ]);
        $this->driver->push(new IntegralJob( $param['user_id'] ));

        return $result;
    }

    /**
     * 会员积分减少
     * @param array $param
     * @return  mixed
     */
    public function integralReduce(array $param)
    {
        $result = $this->integral_detail_dao->create([
            'user_id' => $param['user_id'],
            'status' => 3,
            'type' => $this->integral_detail_dao::CONSUME_TYPE,
            'num' => filterMoney($param['num'],4),
            'remark' => $param['remark'],
        ]);
        $this->driver->push(new IntegralJob( $param['user_id'] ));

        return $result;
    }

    /**
     * 会员设置为渠道商
     * @param int $user_id
     * @return mixed
     */
    public function serUserChannel(int $user_id)
    {
        $user = $this->user_dao->first($user_id);

        if( $user->group_id == 2 ) {
            $this->error("USER.STATUS_EXCEPTION");
        }

        $result = $this->user_dao->retrieve([
            'group_id' => 2,
            'parent_id' => 0
        ],[
            'id' => $user_id
        ]);

        return $result;
    }

    /**
     * 查询会员下级用户
     * @param int $user_id
     * @return mixed
     */
    public function lowerLevel(int $user_id)
    {
        $this->user_dao->lowerLevel([
            'parent_id' => $user_id
        ]);
    }

}